package com.xijing.view;

import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.JTextField;

import com.xijing.model.RoomType;
import com.xijing.service.RoomTypeService;

public class RoomTypeEditFrame extends JDialog implements ActionListener {
	
	private JButton btn_Save,btn_Cannel;
	private JTextField JTF_name,JTF_no,JTF_mark;
	private JLabel JL_no,JL_name,JL_mark;
	private String ID;
	private RoomTypeService Service =new RoomTypeService();
	public RoomTypeEditFrame(JFrame f,Boolean b,String id)
	{
		super(f,"房间类型编辑",b);
		setTitle("房间类型编辑");
		setLayout(null);
		ID=id;
		this.setBounds(new Rectangle(300, 80, 500, 300));
		JTF_no= new JTextField();
		JTF_name= new JTextField();
		JTF_mark= new JTextField();
        btn_Save= new JButton("确定");
		btn_Cannel= new JButton("取消");
		JL_no= new JLabel("类型ID：");
		JL_name= new JLabel("类型名称：");
		JL_mark= new JLabel("备注：");
		
		JL_no.setBounds(20, 20, 80, 20);
		JTF_no.setBounds(110, 20, 110, 20);
		JL_name.setBounds(230, 20, 80, 20);
		JTF_name.setBounds(310, 20, 110, 20);
		JL_mark.setBounds(20, 50, 110, 20);
		JTF_mark.setBounds(110, 50, 290, 20);
		
		btn_Save.setBounds(310, 200, 80, 20);
		btn_Cannel.setBounds(400, 200, 80, 20);
		
		add(JL_no);
		add(JTF_no);
		add(JL_name);
		add(JTF_name);
		add(JL_mark);
		add(JTF_mark);
		add(btn_Save);
		add(btn_Cannel);
		btn_Save.addActionListener(this);
		btn_Cannel.addActionListener(this);
		init(ID);
		this.setVisible(true);
	}
	
	public void init(String ID)
	{
		if(ID==null)
			return;
		RoomType room = Service.getRoomType(ID);
		if(room!=null)
		{
			JTF_no.setText(String.valueOf(room.getSRT_ID()));
			JTF_no.setEnabled(false);
			JTF_name.setText(room.getSRT_Name());
			JTF_mark.setText(room.getSRT_Remark());
		}
		
	}
	
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==btn_Save)
		{
			RoomType mod= new RoomType();
			mod.setSRT_ID(Integer.parseInt(JTF_no.getText()));
			mod.setSRT_Name(JTF_name.getText());
			mod.setSRT_Remark(JTF_mark.getText());
			if(ID==null)
			{
				int num= Service.insertRoomType(mod);
				if(num!=0)
				{
					this.dispose();
				}
				else
				{
					
				}
				
			}
			else
			{
				int num= Service.updateRoomType(mod);
				if(num!=0)
				{
					this.dispose();
				}
			}
		}
		if(e.getSource()==btn_Cannel)
		{
			
		}
	}
	

}
